import DataProvider from "../db/DataProvider.js"
import Cipher from "../cipher/Cipher.js";
export default function login(router) {
    router.post("/login", async function(ctx, next) {
        const provide = new DataProvider();
        const body = ctx.request.body
        const user = await provide.findOne('user', { username: body.username })
        if (user) {
            if (user.password === Cipher.getInstance().decrypt(body.password)) {
                ctx.body = {
                    flag: true,
                    token: "1233344",
                    user: user
                }
            } else {
                ctx.body = {
                    flag: false,
                    msg: "用户密码不正确"
                }
            }
        } else {
            ctx.body = {
                flag: false,
                msg: "用户不存在"
            }
        }
        await next()
    })

    return router.routes()
}